import decimal
import time
import os
from multiprocessing import Process
from openpyxl import Workbook
from openpyxl.styles import PatternFill

from db import *
from spider_xue.utils import EMA


def sub_work(stock_list):
    for symbol in stock_list:
        # print(symbol)
        filename = 'E:/data1203/' + symbol + '.txt'
        lines = []
        if not os.path.exists(filename):
            print(filename)
            continue
        with open(filename, 'r', encoding='GBK') as f:
            for line in f.readlines():
                if '通达信' in line:
                    continue
                lines.append(line.strip().split(' '))

        datetime_list = []
        five_time_dict = {}
        last_date_dict = {}
        last = ''
        for i in range(4, len(lines), 5):
            line = lines[i]
            datee = line[0]
            if datee not in datetime_list:
                datetime_list.append(datee)
                five_time_dict[datee] = {
                    'vol': 0, 'time': datee + ' ' + line[1], 'price': float(line[5])}
                last_date_dict[datee] = last
                last = datee

            vol = sum([float(x[7]) for x in lines[i-4: i+1]])

            if vol > five_time_dict[datee]['vol']:
                five_time_dict[datee] = {
                    'vol': vol, 'time': datee + ' ' + line[1], 'price': float(line[5])}

        dt = ''
        dayy = data_day_db.find_one({"symbol": symbol})

        liang = 0

        same_count = 3
        kai_dict = {}
        ll = []

        for i in range(245, len(lines), 2):
            line = lines[i]
            datee = line[0]
            # print(datee)
           
            if datee != dt:
                liang = 0
                same_count = 3
                
                if ll:
                    opopd = {"symbol": ll[-1]['symbol'], 'ts': ll[-1]['ts'], 'macd': ll[-1]['macd'], 'update_time': ll[-1]['update_time'], 'high': ll[-1]['high'], 'sons': []}
                    for npp in ll:
                        npp['ts'] = npp['update_time']
                        opopd['sons'].append(npp)
                    record_yes_db.update_one({"symbol": ll[-1]['symbol'], 'ts': ll[-1]['ts']}, {"$set": opopd},upsert=True)
                    ll = []

            if datee == dt and same_count == 0:
                continue
            liang += float(line[7])
            vol = sum([float(x[7]) for x in lines[i-4: i+1]])
            if last_date_dict.get(datee) == '' or not five_time_dict.get(last_date_dict[datee]): 
                print(symbol, 2222222222222222, datee)
                continue
            if five_time_dict[last_date_dict[datee]]['vol'] < 1:
                print(symbol, 1111111111111111, datee)
                continue
            p5 = round(vol /five_time_dict[last_date_dict[datee]]['vol'], 2)
            if last_date_dict[datee] and p5 >= 2 and float(line[5]) - float(lines[i-4][2]) > 0:
                dt = datee
                same_count -= 1
                lp = 0
                tsx = int(time.mktime(time.strptime(datee, "%Y-%m-%d"))) * 1000

                if tsx > dayy['item'][-1][0]:
                    continue
                for ddday in range(0, len(dayy['item'])):
                    if dayy['item'][ddday][0] == tsx:
                        lp = ddday
                        break
                if datee not in kai_dict:
                    kai = (dayy['item'][lp][2] - dayy['item']
                           [lp-1][5]) / dayy['item'][lp-1][5] * 100
                    kai_dict[datee] = kai
                else:
                    kai = kai_dict[datee]

                if kai < -3:
                    continue

                fg = EMA([round(decimal.Decimal(x[5]), 2)
                         for x in dayy['item'][:lp]])

                ct = 0
                ct1 = 0
                ct2 = 0
                ct3 = 0
                ppp = 0
                high = 0
                fu_flag = []
                continue_flag = 0

                for zzzz in dayy['item'][lp-10:lp]:
                    if zzzz[7] < -1:
                        fu_flag.append(zzzz[7])
                    else:
                        fu_flag = []

                    if len(fu_flag) == 4:
                        continue_flag = 1
                        break

                    if zzzz[7] < 0:
                        ct += 1

                    if zzzz[7] < 0.5:
                        ct1 += 1

                    if zzzz[7] < 1:
                        ct2 += 1

                    if zzzz[7] < 2:
                        ct3 += 1
                    ppp += zzzz[7]

                if ppp > 15 or ppp < -12:
                    continue
                if continue_flag or ct > 6:
                    continue
                if ct1 >= 7:
                    continue

                if ct1 < 7:
                    high = 1

                if ct2 < 7:
                    high = 2

                if ct3 < 7:
                    high = 3

                if dayy['item'][lp-1][7] < -8:
                    continue

                lassst = dayy['item'][lp-1][5]
                
                lol = round((float(line[5])-lassst) / lassst * 100, 2)
                if lol > 8 or lol < -4 or round((float(line[5])-float(dayy['item'][lp][2])) / float(dayy['item'][lp][2]) * 100, 2) < -3:
                    continue

                ts = int(time.mktime(time.strptime(
                    datee + ' ' + line[1], "%Y-%m-%d %H%M")))
                
                ts1 = int(time.mktime(time.strptime(
                    datee , "%Y-%m-%d")))

                ll.append({"symbol": symbol, 'ts': ts1,'macd': fg, 'update_time': ts, 'high': high, 
                "p5": p5, 'price': float(line[5]), 'liang': liang, 'range': round(lol, 2), 'date': datee + ' ' + line[1]})

        if ll:
            # print(ll[0])
            opopd = {'macd': ll[-1]['macd'], 'update_time': ll[-1]['update_time'], 'high': ll[-1]['high'], 'sons': []}
            for npp in ll:
                npp['ts'] = npp['update_time']
                opopd['sons'].append(npp)
            record_yes_db.update_one({"symbol": ll[-1]['symbol'], 'ts': ll[-1]['ts']}, {"$set": opopd},upsert=True)
            ll = []

# def set_east_five():
    
        # east_history_five_db.update_one(
        #     {"symbol": symbol}, {"$set": {'five_time_dict': five_time_dict}}, upsert=True)

def get_date_excel():
    all_sto_list = stock_db.find({"use": 1})
    book = Workbook()
    sheet1 = book.create_sheet(index=0, title="Mysheet")
    sheet2 = book.create_sheet(index=0, title="Mysheet2")
    sheet3 = book.create_sheet(index=0, title="Mysheet3")
    filename = 'E:/data1203/' + 'SZ002882' + '.txt'
    date_lines = ['2021-07-23', '2021-07-26', '2021-07-27', '2021-07-28', '2021-07-29', '2021-07-30', '2021-08-02', '2021-08-03', '2021-08-04', '2021-08-05', '2021-08-06', '2021-08-09', '2021-08-10', '2021-08-11', '2021-08-12', '2021-08-13', '2021-08-16', '2021-08-17', '2021-08-18', '2021-08-19', '2021-08-20', '2021-08-23', '2021-08-24', '2021-08-25', '2021-08-26', '2021-08-27', '2021-08-30', '2021-08-31', '2021-09-01', '2021-09-02', '2021-09-03', '2021-09-06', '2021-09-07', '2021-09-08', '2021-09-09', '2021-09-10', '2021-09-13', '2021-09-14', '2021-09-15', '2021-09-16', '2021-09-17', '2021-09-22', '2021-09-23', '2021-09-24', '2021-09-27', '2021-09-28', '2021-09-29', '2021-09-30', '2021-10-08', '2021-10-11', '2021-10-12', '2021-10-13', '2021-10-14', '2021-10-15', '2021-10-18', '2021-10-19', '2021-10-20', '2021-10-21', '2021-10-22', '2021-10-25', '2021-10-26', '2021-10-27', '2021-10-28', '2021-10-29', '2021-11-01', '2021-11-02', '2021-11-03', '2021-11-04', '2021-11-05', '2021-11-08', '2021-11-09', '2021-11-10', '2021-11-11', '2021-11-12', '2021-11-15', '2021-11-16', '2021-11-17', '2021-11-18', '2021-11-19', '2021-11-22', '2021-11-23', '2021-11-24', '2021-11-25', '2021-11-26', '2021-11-29', '2021-11-30', '2021-12-01', '2021-12-02']
    # with open(filename, 'r', encoding='GBK') as f:
    #     for line in f.readlines():
    #         if '通达信' in line:
    #             continue
    #         dateee = line.strip().split(',')[0]
    #         if dateee not in date_lines:
    #             date_lines.append(dateee)
    # sheet = book.get_active_sheet('f')
    for i in range(len(date_lines)):
        dr = sheet1.cell(row=1, column=2+i)
        dr.value = date_lines[i]
        dr = sheet2.cell(row=1, column=2+i)
        dr.value = date_lines[i]

    symbol_date = {}
    for stock in all_sto_list:
        rs = east_record_db.find({'symbol': stock['symbol']}).sort([('date', 1)])
        symbol_date[stock['symbol']] = []
        data_list = []
        for rr in rs:
            if rr['date'] not in data_list:
                symbol_date[stock['symbol']].append(rr)
                data_list.append(rr['date'])
            

    d_l = [[k, v] for k, v in symbol_date.items() if len(v) > 0]
    d_l = sorted(d_l, key=lambda x: len(x[1]))

    red_fill = PatternFill(fill_type='solid', fgColor="ff0000")
    green_fill = PatternFill(fill_type='solid', fgColor="31B404")

    one = {}
    two = {}
    three = {}
    four = {}
    ll_five = []
    
    for i in range(len(d_l)-1, 0, -1):
        dr1 = sheet1.cell(row=2+i, column=1)
        dr3 = sheet2.cell(row=2+i, column=1)
        dr1.value = d_l[i][0]
        dr3.value = d_l[i][0]
        for xz in range(len(d_l[i][1])):
            z = d_l[i][1][xz]
            
            
            mv = date_lines.index(z['date'])
            flag1 = False
            flag2 = True
            if xz >= 1:
                mv1 = date_lines.index(d_l[i][1][xz-1]['date'])
                if mv - mv1 == 1:
                    flag1 = True
            # if xz < len(d_l[i][1])-1:
            #     mv2 = date_lines.index(d_l[i][1][xz+1]['date'])
            #     if mv2- mv == 1:
            #         flag2 = True
            if not flag1 and not flag2:
                continue
            dr2 = sheet1.cell(row=2+i, column=2+mv)
            dr4 = sheet2.cell(row=2+i, column=2+mv)
            dr2.value = z['date']
            z['l2'] = z['l3']
            dr4.value = z['l2']
            if z['l2'] > 0:
                dr4.fill = red_fill
            else:
                dr4.fill = green_fill

            if int(z['l2']) not in one:
                one[int(z['l2'])] = 0

            one[int(z['l2'])] += 1

            if int(z['l2']) not in two:
                two[int(z['l2'])] = 0
            if z['high'] >= 1:
                two[int(z['l2'])] += 1

            if int(z['l2']) not in three:
                three[int(z['l2'])] = 0
            if z['high'] >= 2:
                three[int(z['l2'])] += 1

            if int(z['l2']) not in four:
                four[int(z['l2'])] = 0

            if z['high'] >= 3:
                four[int(z['l2'])] += 1

            if int(z['l2']) not in ll_five:
                ll_five.append(int(z['l2']))

    # one = sorted([{'k': k, 'v':v} for k, v in one.items() if k < 50], key=lambda x: x['k'])
    # two = sorted([{'k': k, 'v':v} for k, v in two.items() if k < 50], key=lambda x: x['k'])
    # three = sorted([{'k': k, 'v':v} for k, v in three.items() if k < 50], key=lambda x: x['k'])
    # four = sorted([{'k': k, 'v':v} for k, v in four.items() if k < 50], key=lambda x: x['k'])
    ll_five = sorted(ll_five)
    for i in range(len(ll_five)):
        dr = sheet3.cell(row=2+i, column=1)
        dr.value = ll_five[i]

        if ll_five[i] in one:
            dr = sheet3.cell(row=2+i, column=2)
            dr.value = one[ll_five[i]]

        if ll_five[i] in two:
            dr = sheet3.cell(row=2+i, column=3)
            dr.value = two[ll_five[i]]

        if ll_five[i] in three:
            dr = sheet3.cell(row=2+i, column=4)
            dr.value = three[ll_five[i]]

        if ll_five[i] in four:
            dr = sheet3.cell(row=2+i, column=5)
            dr.value = four[ll_five[i]]

    book.save(f"fff5.xlsx")


if __name__ == '__main__':

    all_sto_list = stock_db.find({"use": 1}) # , "symbol": "SZ002245"
    count = 0
    stock_list = []
    pp = []
    for stock in all_sto_list:
        stock_list.append(stock['symbol'])
        if len(stock_list) == 100:
            p = Process(target=sub_work, args=(stock_list,))
            p.daemon = True
            p.start()
            pp.append(p)
            count += 1
            print(count)
            stock_list = []
    if stock_list:
        p = Process(target=sub_work, args=(stock_list,))
        p.daemon = True
        p.start()
        pp.append(p)

    for p in pp:
        p.join()
    # get_date_excel()
